package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/constant"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type TraceabilityFindOneLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTraceabilityFindOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TraceabilityFindOneLogic {
	return &TraceabilityFindOneLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TraceabilityFindOneLogic) TraceabilityFindOne(in *cardclient.TraceabilityFindOneRequest) (*cardclient.TraceabilityFindOneResponse, error) {
	var ctx context.Context
	data, err := l.svcCtx.TraceabilityModel.FindOne(ctx, &entity.CardTraceabilityReq{
		Id:         in.Id,
		CardNumber: in.CardNumber,
	})
	if err != nil {
		return nil, err
	}

	traceability := &cardclient.TraceabilityFindOneResponse{
		Id:             data.Id,
		CardNumber:     data.CardNumber,
		StorageAdminId: data.StorageAdminId,
		StorageTime:    data.StorageTime.Format(constant.YMDHIS),
		PublishAdminId: data.PublishAdminId,
		PublishTime:    data.PublishTime.Format(constant.YMDHIS),
		ReceiveAdminId: data.ReceiveAdminId,
		ReceiveTime:    data.ReceiveTime.Format(constant.YMDHIS),
		BindingUserId:  data.BindingUserId,
		BindingTime:    data.BindingTime.Format(constant.YMDHIS),
		WriteOffTime:   data.WriteOffTime.Format(constant.YMDHIS),
		Status:         data.Status,
		CreateAt:       data.CreateAt.Format(constant.YMDHIS),
		UpdateAt:       data.UpdateAt.Format(constant.YMDHIS),
		DeleteAt:       data.DeleteAt.Format(constant.YMDHIS),
	}

	return traceability, nil

}
